package org.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.samples.model.ExamSubject;
import org.samples.model.ExamUser;
import org.samples.model.vo.ExamScoreVO;
import org.samples.model.vo.ExamSubjectVO;
import org.samples.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value = "")
	public String toStudent(HttpSession session, Model model){
		ExamUser user = (ExamUser) session.getAttribute("user");
		List<ExamSubjectVO> subjectList = studentService.getSubjectList(user);
		model.addAttribute("subjectList", subjectList);
		return "student/subjects";
	}
	
	@RequestMapping(value = "/subjects")
	public String toSubjects(HttpSession session, Model model){
		ExamUser user = (ExamUser) session.getAttribute("user");
		List<ExamSubjectVO> subjectList = studentService.getSubjectList(user);
		model.addAttribute("subjectList", subjectList);
		return "student/subjects";
	}
	
	@ResponseBody
	@RequestMapping(value = "/showScore", method = RequestMethod.POST)
	public ExamScoreVO showScore(HttpSession session, Integer scoreId){
		return studentService.getScoreById(scoreId);
	}
	
	@RequestMapping(value = "/toTest/{subId}")
	public String toTest(HttpSession session, Model model, @PathVariable Integer subId){
		model.addAttribute("testListMap", studentService.getTestListBySubject(subId, ((ExamUser)session.getAttribute("user")).getUserId()));
		model.addAttribute("subject", studentService.getSubjectVOById(((ExamUser)session.getAttribute("user")).getUserId(),subId));
		return "student/testPage";
	}
	
	@RequestMapping(value = "/assignment", method = RequestMethod.POST)
	public String assignment(HttpServletRequest request, Integer subId, RedirectAttributes redirectattr){
		Map<String, String[]> parameterMap = request.getParameterMap();
		Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
		List<Integer> answerIds = new ArrayList<Integer>();
		Map<String, String[]> testMap = new HashMap<String, String[]>();
		if(entrySet != null){
			Iterator<Entry<String, String[]>> it = entrySet.iterator();
			final String PREFIX = "test";
			while(it.hasNext()){
				Entry<String, String[]> entry = it.next();
				String key = entry.getKey();
				String[] splitArrayString = key.split("_");
				if(splitArrayString.length == 2){
					if(PREFIX.equals(splitArrayString[0])){
						answerIds.add(Integer.valueOf(splitArrayString[1]));
						testMap.put(splitArrayString[1], entry.getValue());
					}
				}
			}
			ExamScoreVO score = studentService.calculateScore(answerIds, testMap, ((ExamUser)request.getSession().getAttribute("user")).getUserId(), subId);
			ExamSubject subject = studentService.getSubjectById(subId);
			redirectattr.addFlashAttribute("score", score);
			redirectattr.addFlashAttribute("subject", subject);
			return "redirect:/student/subjects";
		}
		return null;
	}
	
}
